package cn.yunhe.dao;

import cn.yunhe.pojo.House;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Component("houseDao2")
public class HouseDao {

    /*@Autowired*/
    @Resource
    /*@Resource(name = "dataSource1")*/
    private DataSource dataSource;

    List<House>houseList=new ArrayList<>();

    public List<House> houseList(){
        try {
            Connection connection=dataSource.getConnection();
            Statement statement= connection.createStatement();
            String sql="select * from house";
            ResultSet resultSet= statement.executeQuery(sql);

            while(resultSet.next()){
                House house=new House();
                house.setHid(resultSet.getInt("hid"));
                house.setHtitle(resultSet.getString("htitle"));
                house.setHprice(resultSet.getDouble("hprice"));
                house.setHfloorage(resultSet.getDouble("hfloorage"));
                house.setHpubdate(resultSet.getString("hpubdate"));
                house.setHcontact(resultSet.getString("hcontact"));
                house.setHdesc(resultSet.getString("hdesc"));
                house.setHflag(resultSet.getInt("hflag"));
                houseList.add(house);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            return houseList;
        }
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
